package com.eastelsoft.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gitee.myclouds.toolbox.util.WebCxt;
import com.gitee.myclouds.toolbox.wrap.Dto;

@RestController
@RequestMapping(value="mybatis")
public class MyBatisController {

	@Autowired
	private SqlSession sqlSession;
	
	@RequestMapping(value="query", method=RequestMethod.GET)
	public String findById(HttpServletRequest request) {
		String id = request.getParameter("id");
		Dto inDto = WebCxt.getParamAsDto(request);
		Dto dto = sqlSession.selectOne("sql.mybatis.queryById", inDto);
		return JSON.toJSONString(dto);
	}
	
	/**
	 * 
	 * @param request(start|length)
	 * @return
	 */
	@RequestMapping(value="queryAll", method=RequestMethod.GET)
	public String queryAll(HttpServletRequest request) {
		Dto inDto = WebCxt.getParamAsDto(request);
		List<Dto> list = sqlSession.selectList("sql.mybatis.pageOrg", inDto);
		return JSON.toJSONString(list);
	}
	
}
